package wycenanieruchomosci;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.swing.filechooser.FileNameExtensionFilter;

public class FileChooserFrame extends javax.swing.JFrame {

    private Map<Integer, List<Integer>> valueMap;
    private WycenaNieruchomosciView mainWindow;
    private List<String> argumentsList;
    private Map<String, List<String>> resultsMap;

    public FileChooserFrame(WycenaNieruchomosciView wycenaNieruchomosci) {
        mainWindow = wycenaNieruchomosci;
        initComponents();
//        csvChooser.setFileFilter(new FileNameExtensionFilter("csv,xls", "csv,xls"));
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        csvChooser = new javax.swing.JFileChooser();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        csvChooser.setDragEnabled(true);
        csvChooser.setName("csvChooser"); // NOI18N
        csvChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                csvChooserActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(csvChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 449, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(csvChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void csvChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_csvChooserActionPerformed
        CsvReader csvReader = new CsvReader(csvChooser.getSelectedFile(), this);
        valueMap = csvReader.getValueMap();
        argumentsList = csvReader.getArgumentsList();
        mainWindow.getCompateButton().setEnabled(true);

        this.hide();
    }//GEN-LAST:event_csvChooserActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
            }
        });
    }

    public void findEqualAndSimmularParametters() {
        resultsMap = new HashMap<String, List<String>>();
        mainWindow.getTextArea().setText("");
        int compareCounter = 0;
        for (Integer choosenKey : valueMap.keySet()) {
            for (Integer secoundKey : valueMap.keySet()) {
                if (secoundKey > choosenKey) {

                    List<Integer> firstList = valueMap.get(choosenKey);
                    List<Integer> secoundList = valueMap.get(secoundKey);

                    int differenceCounter = 0;
                    int lastDifferenceValue = 0;
                    String lastDifference = "";

                    for (int i = 0; i < firstList.size(); i++) {
                        if (!firstList.get(i).equals(secoundList.get(i))) {
                            differenceCounter++;
                            lastDifference = argumentsList.get(i + 1);
                            lastDifferenceValue = firstList.get(i) - secoundList.get(i);
                            compareCounter++;
                        }
                    }
                    if (differenceCounter == 0) {
                        if (resultsMap.get("identyczne") == null) {
                            List<String> list = new LinkedList<String>();
                            list.add("Wiersze " + choosenKey + " oraz " + secoundKey + " maja identyczne argumenty \n");
                            resultsMap.put("identyczne", list);
                        } else {
                            //mainWindow.getTextArea().append("Wiersze " + choosenKey + " oraz " + secoundKey + " maja identyczne argumenty \n");
                            resultsMap.get("identyczne").add("Wiersze " + choosenKey + " oraz " + secoundKey + " maja identyczne argumenty \n");
                        }
                    } else if (differenceCounter == 1) {
                        if (resultsMap.get(lastDifference) == null) {
                            List<String> list = new LinkedList<String>();
                            list.add("Wiersze " + choosenKey + " oraz " + secoundKey + "  << różnica wynosi " + lastDifferenceValue + "\n");
                            resultsMap.put(lastDifference, list);

//                            mainWindow.getTextArea().append("Wiersze " + choosenKey + " oraz " + secoundKey + " różnią się jedynie argumentem : >>  " + lastDifference + "  << różnica wynosi " + lastDifferenceValue + "\n");
                        } else {
                            resultsMap.get(lastDifference).add("Wiersze " + choosenKey + " oraz " + secoundKey + "  << różnica wynosi " + lastDifferenceValue + "\n");
                        }
                    }
                }
            }
        }

        printData();

        mainWindow.getTextArea().append("wykonano porównań:  " + compareCounter);
    }

    private void printData() {

        for (String key : resultsMap.keySet()) {
            mainWindow.getTextArea().append(key + "\n");
            for (String data : resultsMap.get(key)) {
                mainWindow.getTextArea().append(data);
            }
        }
    }

    public void writeLinieOnConsole(String information) {
        mainWindow.getTextArea().append(information);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFileChooser csvChooser;
    // End of variables declaration//GEN-END:variables
}
